<!DOCTYPE html>
<html>
<head>
    <title>文章新建</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
    <form class="layui-form" autocomplete="off">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">作者</label>
                        <div class="layui-form">
                            <div class="layui-input-inline">
                                <select name="author" id="" lay-filter="mySelect">
                                    {% for author in author_list %}
                                    <option value="{{ author.realname }}">{{ author.realname }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类</label>
                        <div class="layui-form" lay-filter="myDiv">
                            <div class="layui-input-inline">
                                <select name="category_id" id="" lay-filter="mySelect">
                                    {% for category in category_list %}
                                    <option value="{{ category.id }}">{{ category.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">正文</label>
                        <div class="layui-input-block">
                            <textarea name="content" placeholder="请输入内容" class="layui-textarea" id="editor"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="article-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    {% include 'admin/common/footer.html' %}
    <script type="text/javascript" src="{{ url_for('static', filename='component/pear/module/tinymce/tinymce/tinymce.min.js') }}"></script>
    <script>
        tinymce.init({
            entity_encoding: "raw",
            branding: false,
            selector: '#editor',
            height: 450,
            width: 1110,
            relative_urls: false, 
            remove_script_host: false,
            convert_urls: false,
            images_upload_url: '/admin/article/upload',
            plugins: "emoticons image becodesample code link",
            toolbar: 'undo redo removeformat bold italic underline strikethrough blockquote subscript superscript forecolor backcolor alignleft aligncenter alignright alignjustify ' +
            'emoticons link image becodesample code',
            content_css: "{{ url_for('static', filename='component/pear/module/tinymce/tinymce/plugins/becodesample/highlight.js-11.5.1/styles/agate.min.css') }}",
            language:'zh_CN',
            setup: function(editor){ editor.on('change',function(){ editor.save(); });},
        });
        layui.use(['form'], function () {
          var form = layui.form;
          form.on('submit(article-save)', function (data) {
            $.ajax({
              url: '/admin/article/save',
              data: JSON.stringify(data.field),
              dataType: 'json',
              contentType: 'application/json',
              type: 'post',
              success: function (result) {
                if (result.success) {
                  layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name))
                    parent.layui.table.reload('article-table')
                  })
                } else {
                  layer.msg(result.msg, { icon: 2, time: 1000 })
                }
              }
            })
            return false
          })
        })
    </script>
</body>
</html>